package com.a.add4thread;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button b;
    Button b2;

    class Runn implements Runnable {
        String ss;

        public Runn(String s) {
            ss = s;
        }

        @Override
        public void run() {
            while (running) {
                //System.out.println("aaaa" + ss);
            }
        }
    }
    volatile boolean running = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Runn runn1 = new Runn("1");
        final Runn runn2 = new Runn("2");
        final Runn runn3 = new Runn("3");
        final Runn runn4 = new Runn("4");

        final Thread t1 = new Thread(runn1);
        final Thread t2 = new Thread(runn2);
        final Thread t3 = new Thread(runn3);
        final Thread t4 = new Thread(runn4);



        b = (Button) findViewById(R.id.btn);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                running = true;
                t1.start();
                t2.start();
                t3.start();
                t4.start();
            }
        });

        b2 = (Button) findViewById(R.id.btn2);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                running = false;
                t1.interrupt();
                t2.interrupt();
                t3.interrupt();
                t4.interrupt();
            }
        });

    }
}
